package cn.wolfcode.shop.mq;

import cn.wolfcode.shop.consts.MQConstents;
import cn.wolfcode.shop.mq.msg.SeckillSuccessDelayMsg;
import cn.wolfcode.shop.service.IOrderInfoService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 秒杀成功之后，用户超过30 分钟没有支付，消费延迟消息：未支付回补库存，清除售完标记等
 */
@Component
@Slf4j
@RocketMQMessageListener(
        topic = MQConstents.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstents.SECKILL_SUCCESS_DELAY_TAG,
        consumerGroup = MQConstents.SECKILL_SUCCESS_DELAY_CONSUMER_GROUP
)
public class SeckillSuccessDelayListener implements RocketMQListener<SeckillSuccessDelayMsg> {
    @Autowired
    private IOrderInfoService orderInfoService;

    @Override
    public void onMessage(SeckillSuccessDelayMsg msg) {
        log.info("收到订单延迟超时的消息：{}", JSON.toJSONString(msg));
        // 检查订单是否超时未支付，如果超时就做回补库存，修改售完标记等处理
        orderInfoService.checkOrderTimeOut(msg.getOrderNo(), msg.getSeckillId());
    }
}
